<?php
class cyitem_orderControl extends skymvc{
	
	public function __construct(){
		parent::__construct();		
	}
	
	public function onInit(){
		M("login")->checklogin();
	}
	
	public function ondefault(){
		
	}
	
	public function onMy(){
		$where=" status<99 AND userid=".M("login")->userid;
		$url="/index.php?m=cyitem_order&a=my";
		$s_status=get('s_status');
		if($s_status){
			switch($s_status){
				case 1:
						$where.=" AND order_status=0";
					break;
				case 2:
						$where.=" AND order_status=1 ";
					break;
				case 3:
				
					$where.=" AND order_status=98 ";
					break;
			}
			$url.="&s_status=".$s_status;
		}
		$statr=get('start','i');
		$limit=20;
		$option=array(
			"where"=> $where,
			"order"=>" id DESC",
			"start"=>$start,
			"limit"=>$limit
			
		);
		$rscount=true;
		$data=M("cyitem_order")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['cyitem']=M("cyitem")->selectRow("id=".$v['object_id']);
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(array(
			"data"=>$data,
			"order_status"=>M("cyitem_order")->order_status(),
			"user"=>$user,
			"pagelist"
		));
		$tpl=M("pagetpl")->get("cyitem_order","my");
		$this->smarty->display($tpl);
	 
	}
	
	public function onCheck(){
		
		$id=get_post('id','i');
		$ordernum=max(1,get_post('ordernum','i'));
		$row=M("cyitem")->selectRow("id=".$id);
		if($row['total_num']<$row['buy_num']+$ordernum){
			exit(json_encode(array("error"=>1,"message"=>"超过融资额度")));
		}
		if($row['endtime']<date("Y-m-d")  or $row['status']!=2){
			exit(json_encode(array("error"=>2,"message"=>"项目融资结束")));
		}
		$user=M("login")->getUser();
		exit(json_encode(array("error"=>0)));
		
	}
	
	public function onConfirm(){
		$id=get_post('id','i');
		$ordernum=get_post('ordernum','i');
		$row=M("cyitem")->selectRow("id=".$id);
		if($row['total_num']<$row['buy_num']+$ordernum){
			$this->goall("超过融资额度",1,0,"/index.php?m=show&id=".$id);
		}
		if($row['endtime']<date("Y-m-d")  or $row['status']!=2){
			$this->goall("项目融资结束",1,1,0,"/index.php?m=show&id=".$id);
		}
		$user=M("login")->getUser();
		$this->smarty->assign(array(
			"cyitem"=>$row,
			"user"=>$user,
			"ordernum"=>$ordernum,
			"money"=>$ordernum*$row['price']
		));
		$tpl=M("pagetpl")->get("cyitem_order","confirm");
		$this->smarty->display($tpl);
		 
	}
	
	public function onBuy(){
		$id=get_post('id','i');
		$ordernum=max(1,get_post('ordernum','i'));
		$row=M("cyitem")->selectRow("id=".$id);
		if($row['total_num']<$row['buy_num']+$ordernum){
			$this->goall("超过融资额度",1,0,"/index.php?m=show&id=".$id);
		}
		if($row['endtime']<date("Y-m-d") or $row['status']!=2){
			$this->goall("项目融资结束",1,0,"/index.php?m=show&id=".$id);
		}
		$user=M("login")->getUser();
		$money=$ordernum*$row['price'];
		$ispay=0;
		if($user['money']>=$money){
			$paypwd=post('paypwd','h');
			if(umd5($paypwd)!=$user['paypwd']){
				$this->goall("支付密码出错",1);
			}
			$this->loadControl("jfapi");
			$jfdata=array(
				"money"=>-$money,
				"content"=>"您投资".$row['title']."花了".$money."元，之前￥[oldmoney]，目前还剩￥[newmoney]",
				"type_id"=>1,
				"ispay"=>1 //1支出 2收入
			 );
			$this->jfapiControl->addMoney($jfdata);
			$ispay=1;
		}
		$data=array(
			"object_id"=>$id,
			"goods_num"=>$ordernum,
			"goods_price"=>$row['price'],
			"money"=>$money,
			"dateline"=>time(),
			"userid"=>M("login")->userid,
			"content"=>"您投资了".$money."元，获得了".($row['gufen_num']*$ordernum)."股份！",
			"ispay"=>$ispay
		);
		$orderid=M("cyitem_order")->insert($data);
		if($orderid){
			M("cyitem")->update(array("buy_num"=>$row['buy_num']+$ordernum),"id=".$id);
		}
		if($ispay){
			$this->goall("感谢您的投资",1,0,R("/index.php?m=cyitem_order&a=my"));
		}else{
			$this->goall("请在两小时内支付订单，否则订单将会被取消！",1,0,"/index.php?m=cyitem_order&a=pay&id=".$orderid);
		}
	}
	
	public function onPay(){
		$id=get_post('id','i');
		$order=M("cyitem_order")->selectRow("id=".$id);
		if(empty($order) or $order['userid']!=M("login")->userid){
			$this->goall("订单出错",1,0,"/index.php?m=cyitem_order&a=my");
		}
		if($order['ispay']){
			$this->goall("已支付",1,0,"/index.php?m=cyitem_order&a=my");
		}
		$cyitem=M("cyitem")->selectRow("id=".$order['object_id']);
		$user=M("login")->getUser();
		$this->smarty->assign(array(
			"order"=>$order,
			"cyitem"=>$cyitem,
			"user"=>$user,
			"order_status"=>M("cyitem_order")->order_status()
		));
		$this->smarty->display("cyitem_order/pay.html");
	}
	
	public function onPaySave(){
		$id=get_post('id','i');
		$order=M("cyitem_order")->selectRow("id=".$id);
		if(empty($order) or $order['userid']!=M("login")->userid){
			$this->goall("订单出错",1,0,"/index.php?m=cyitem_order&a=my");
		}
		if($order['ispay']){
			$this->goall("已支付",1,0,"/index.php?m=cyitem_order&a=my");
		}
		$user=M("login")->getUser();
		$paypwd=post('paypwd','h');
		if($user['money']>=$order['money']){
			$paypwd=post('paypwd','h');
			if(umd5($paypwd)!=$user['paypwd']){
				$this->goall("支付密码出错",1);
			}
			$this->loadControl("jfapi");
			$jfdata=array(
				"money"=>-$order['money'],
				"content"=>"您投资".$row['title']."花了".$order['money']."元，之前￥[oldmoney]，目前还剩￥[newmoney]",
				"type_id"=>1,
				"ispay"=>1 //1支出 2收入
			 );
			$this->jfapiControl->addMoney($jfdata);
			M("cyitem_order")->update(array("ispay"=>1),"id=".$id);
			$this->goall("支付成功",0,0,"/index.php?m=cyitem_order&a=my");
		}else{
			$this->goall("余额不足",1);
		}
	}
	
}
?>